<template>
   
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="用户ID：">
            <el-input
              v-model="listQuery.uuid"
              class="input-width"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="用户昵称：">
            <el-input
              v-model="listQuery.nickname"
              class="input-width"
              placeholder="模糊查询"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="用户类型：">
            <el-select
              v-model="listQuery.role"
              placeholder="全部"
              clearable
              class="input-width"
            >
              <el-option
                v-for="item in roleOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="职业：">
            <el-input
              v-model="listQuery.job"
              class="input-width"
              placeholder="模糊查询"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="帐号启用状态：">
            <el-select
              v-model="listQuery.status"
              placeholder="全部"
              clearable
              class="input-width"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别：">
            <el-select
              v-model="listQuery.gender"
              placeholder="全部"
              clearable
              class="input-width"
            >
              <el-option
                v-for="item in genderOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="教育水平：">
            <el-select
              v-model="listQuery.education"
              placeholder="全部"
              clearable
              class="input-width"
            >
              <el-option
                v-for="item in educationOptions"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所在省份：">
            <el-input
              v-model="listQuery.keyword"
              class="input-width"
              placeholder="模糊查询"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="所在城市：">
            <el-input
              v-model="listQuery.city"
              class="input-width"
              placeholder="模糊查询"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="所在区/县：">
            <el-input
              v-model="listQuery.district"
              class="input-width"
              placeholder="模糊查询"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-tabs v-model="statusTab" type="card" @tab-click="handleClickTabs">
        <el-tab-pane
          :label="'全部(' + countMap.allCount + ')'"
          name="-1"
        ></el-tab-pane>
        <el-tab-pane
          :label="'审核通过(' + countMap.reviewedCount + ')'"
          name="30"
        ></el-tab-pane>
        <el-tab-pane
          :label="'审核中(' + countMap.underReviewCount + ')'"
          name="10"
        ></el-tab-pane>
        <el-tab-pane
          :label="'未通过(' + countMap.notPassedCount + ')'"
          name="0"
        ></el-tab-pane>
      </el-tabs>
      <el-table
        ref="userTable"
        @selection-change="handleSelectionChange"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column label="用户ID" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.uuid }}</template>
        </el-table-column>
        <el-table-column label="头像" width="100" align="center">
          <template slot-scope="scope"
            ><img style="height: 80px" :src="scope.row.avatar" alt="未设置"
          /></template>
        </el-table-column>
        <el-table-column label="昵称" align="center">
          <template slot-scope="scope">{{ scope.row.nickname }}</template>
        </el-table-column>
        <el-table-column label="用户类型" width="100" align="center">
          <template slot-scope="scope">{{
            scope.row.role | roleFilter
          }}</template>
        </el-table-column>
        <!-- <el-table-column label="工作" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.job }}</template>
        </el-table-column>
        <el-table-column label="个人简介" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.personalSign }}</template>
        </el-table-column> -->
        <el-table-column label="性别" width="50" align="center">
          <template slot-scope="scope">{{
            scope.row.gender | genderFilter
          }}</template>
        </el-table-column>
        <el-table-column label="年龄" width="50" align="center">
          <template slot-scope="scope">{{ scope.row.age }}</template>
        </el-table-column>
        <el-table-column label="教育水平" width="100" align="center">
          <template slot-scope="scope">{{
            scope.row.education | educationFilter
          }}</template>
        </el-table-column>
        <!-- <el-table-column label="地址" width="150" align="center">
          <template slot-scope="scope">{{
            scope.row.province + scope.row.city + scope.row.district
          }}</template>
        </el-table-column> -->
        <el-table-column label="注册时间" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.registerTime }}</template>
        </el-table-column>
        <el-table-column label="审核状态" width="160" align="center">
          <template slot-scope="scope">
            <div :class="verifyStatusColorFilter(scope.row.verifyStatus)">
              {{ scope.row.verifyStatus | verifyStatusFilter }}
            </div></template
          >
        </el-table-column>
        <el-table-column label="是否启用" width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleViewDetail(scope.$index, scope.row)"
              >查看详情
            </el-button>
            <el-button
              size="mini"
              type="text"
              class="color-danger"
              @click="handleVerify(scope.$index, scope.row)"
            >
              审核
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small"
      >
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 15, 20]"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="审核" :visible.sync="verifyDialogVisible" width="30%">
      <el-form :model="verifyForm" label-width="80px">
        <el-form-item label="审核结果">
          <el-radio-group v-model="verifyForm.verifyStatus" size="medium">
            <el-radio :label="30">通过</el-radio>
            <el-radio :label="0">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入内容"
            v-model="verifyForm.detail"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleVerifySubmit" size="small"
          >确 定</el-button
        >
        <el-button @click="verifyDialogVisible = false" size="small"
          >取 消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  fetchList,
  updateStatus,
  getCounts,
  updateVerifyStatus,
} from "@/api/user";
import { getEducations } from "@/api/enum";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  uuid: null,
  nickname: null,
  status: null,
  job: null,
  gender: null,
  education: null,
  province: null,
  city: null,
  district: null,
  sortBy: null,
  verifyStatus: null,
};

const defaultCountMap = {
  allCount: 0,
  underReviewCount: 0,
  reviewedCount: 0,
  notPassedCount: 0,
};
export default {
  name: "userList",
  data() {
    return {
      statusTab: "-1",
      countMap: Object.assign({}, defaultCountMap),
      operates: [
        {
          label: "批量审核",
          value: "verify",
        },
      ],
      operateType: "verify",
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      dialogVisible: false,
      statusOptions: [
        {
          label: "启用",
          value: 1,
        },
        {
          label: "封禁",
          value: 0,
        },
      ],
      genderOptions: [
        {
          label: "男",
          value: 1,
        },
        {
          label: "女",
          value: 0,
        },
        {
          label: "未设置",
          value: 2,
        },
      ],
      educationOptions: [],
      roleOptions: [
        {
          label: "注册用户",
          value: 0,
        },
        {
          label: "实名用户",
          value: 1,
        },
        {
          label: "测试用户",
          value: 2,
        },
      ],
      verifyDialogVisible: false,
      verifyForm: {
        uuids: [],
        verifyStatus: 30,
        detail: "",
      },
      multipleSelection: [],
    };
  },
  created() {
    this.getList();
    this.getEducationOptions();
    this.getCountMap();
  },
  filters: {
    roleFilter(role) {
      if (role === 0) {
        return "注册用户";
      } else if (role === 1) {
        return "实名用户";
      } else if (role === 2) {
        return "测试账户";
      }
    },
    genderFilter(gender) {
      if (gender === 0) {
        return "女";
      } else if (gender === 1) {
        return "男";
      } else {
        return "未知";
      }
    },
    educationFilter(education) {
      // todo 为减少结构调整，这里写死
      // 优化方向：后端获取枚举列表
      if (education === 0) {
        return "小学";
      } else if (education === 1) {
        return "初中";
      } else if (education === 2) {
        return "高中";
      } else if (education === 3) {
        return "大专";
      } else if (education === 4) {
        return "本科";
      } else if (education === 5) {
        return "硕士";
      } else if (education === 6) {
        return "博士";
      } else {
        return "未知";
      }
    },
    verifyStatusFilter(verifyStatus) {
      if (verifyStatus === 30) {
        return "已通过";
      } else if (verifyStatus === 0) {
        return "未通过";
      } else if (verifyStatus === 10) {
        return "审核中";
      }
    },
  },
  methods: {
    verifyStatusColorFilter(verifyStatus) {
      if (verifyStatus === 30) {
        return "color-success";
      } else if (verifyStatus === 0) {
        return "color-danger";
      } else if (verifyStatus === 10) {
        return "color-main";
      }
    },
    handleClickTabs(tab, event) {
      // console.log(this.statusTab);
      var verifyStatus = Number(this.statusTab);
      if (verifyStatus !== -1) {
        this.listQuery.verifyStatus = verifyStatus;
      } else {
        this.listQuery.verifyStatus = null;
      }
      this.getList();
      this.getCountMap();
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    getEducationOptions() {
      getEducations().then((res) => {
        this.educationOptions = res.data;
      });
    },
    handleStatusChange(index, row) {
      this.$confirm("是否要修改该状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          updateStatus(row.id, { status: row.status }).then((response) => {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消修改",
          });
          this.getList();
        });
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    getCountMap() {
      getCounts(this.listQuery).then((res) => {
        this.countMap = res.data;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleBatchOperate() {
      if (this.operateType == null) {
        this.$message({
          message: "请选择操作类型",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      if (this.multipleSelection == null || this.multipleSelection.length < 1) {
        this.$message({
          message: "请选择要操作的视频",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      let uuids = [];
      for (let i = 0; i < this.multipleSelection.length; i++) {
        uuids.push(this.multipleSelection[i].uuid);
      }
      this.verifyDialogVisible = true;
      this.verifyForm.uuids = uuids;
    },
    handleViewDetail(index, row) {
        this.$router.push({
        path: "/crm/userDetail",
        query: { uuid: row.uuid },
      });
    },
    handleVerify(index, row) {
      this.verifyDialogVisible = true;
      this.verifyForm.uuids = [row.uuid];
    },
    handleVerifySubmit() {
      let params = new URLSearchParams();
      params.append("uuids", this.verifyForm.uuids);
      params.append("verifyStatus", this.verifyForm.verifyStatus);
      params.append("detail", this.verifyForm.detail);
      updateVerifyStatus(params).then((res) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
        this.verifyDialogVisible = false;
        this.getList();
        this.getCountMap();
      });
    },
  },
};
</script>
<style></style>


